//
//  MVCVC.m
//  Demo_MVC
//
//  Created by 陈冰 on 2020/3/8.
//  Copyright © 2020 ChenBing. All rights reserved.
//

#import "MVVMVC.h"
#import "MVVMTableViewCell.h"
#import "MVVMModel.h"
#import "MVVMDataSource.h"
#import "MVVMViewModel.h"

static NSString *const cellRI = @"CellReuseIdentifier";
@interface MVVMVC ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) MVVMDataSource *tvDataSrouce;
@property (nonatomic, strong) MVVMViewModel *mvVM;

@end

@implementation MVVMVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MVVMTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellRI];
    // 创建TableView操作绑定
    __weak typeof(self) weakSelf = self;
    self.tvDataSrouce = [[MVVMDataSource alloc] initWithCellResueIdentifier:cellRI cellConfigBeforeBlock:^(MVVMTableViewCell *cell, MVVMModel *model, NSIndexPath * _Nonnull indexPath) {
        cell.nameLab.text = model.name;
        cell.numLab.text = model.num;
        cell.indexPath = indexPath;
        cell.delegate = weakSelf.mvVM;
    } selectCellBlock:^(NSIndexPath * _Nonnull indexPath) {
        
    } reloadDataBlock:^(NSMutableArray * _Nonnull array) {
        weakSelf.mvVM.dataArray = array;
    }];
    // 绑定数据源
    self.tableView.dataSource = self.tvDataSrouce;
    self.tableView.delegate = self.tvDataSrouce;
    // 生成ViewModel
    self.mvVM = [[MVVMViewModel alloc] initWithSuccessBlock:^(NSArray *data) {
        [weakSelf.tvDataSrouce addDataToArray:data];
        [weakSelf.tableView reloadData];
    } failBlock:^(id  _Nonnull data) {
            
    }];
    [self.mvVM loadData];
}

@end
